package com.qiangesoft.design.creational.builder;

/**
 * 建造者模式
 */
public class Builder {
    public static void main(String[] args) {
        // 使用建造者模式创建不同类型的房屋，每种房屋类型的建造过程都由相应的具体建造者类负责实现，而指导者类负责协调建造过程。
        HouseBuilder concreteBuilder = new ConcreteHouseBuilder();
        Director director1 = new Director(concreteBuilder);
        House concreteHouse = director1.constructHouse();
        System.out.println("Concrete House: " + concreteHouse);

        HouseBuilder luxuryBuilder = new LuxuryHouseBuilder();
        Director director2 = new Director(luxuryBuilder);
        House luxuryHouse = director2.constructHouse();
        System.out.println("Luxury House: " + luxuryHouse);
    }
}

/**
 * 1.定义房屋类 House
 * 它具有多个属性，如地基、结构、屋顶和装修
 */
class House {
    /**
     * 地基
     */
    private String foundation;
    /**
     * 结构
     */
    private String structure;
    /**
     * 屋顶
     */
    private String roof;
    /**
     * 装修
     */
    private String interior;

    public void setFoundation(String foundation) {
        this.foundation = foundation;
    }

    public void setStructure(String structure) {
        this.structure = structure;
    }

    public void setRoof(String roof) {
        this.roof = roof;
    }

    public void setInterior(String interior) {
        this.interior = interior;
    }

    @Override
    public String toString() {
        return "House{" +
                "foundation='" + foundation + '\'' +
                ", structure='" + structure + '\'' +
                ", roof='" + roof + '\'' +
                ", interior='" + interior + '\'' +
                '}';
    }
}

/**
 * 2.创建一个抽象建造者类 HouseBuilder
 * 它定义了构建房屋的方法
 */
abstract class HouseBuilder {
    protected House house = new House();

    /**
     * 打地基
     */
    public abstract void buildFoundation();

    /**
     * 修结构
     */
    public abstract void buildStructure();

    /**
     * 修屋顶
     */
    public abstract void buildRoof();

    /**
     * 安装修
     */
    public abstract void buildInterior();

    public House getHouse() {
        return house;
    }
}

/**
 * 3.创建两个具体的建造者类
 * ConcreteHouseBuilder（混凝土房子）、LuxuryHouseBuilder（豪华房子）分别实现了不同类型房屋的构建过程
 */
class ConcreteHouseBuilder extends HouseBuilder {
    @Override
    public void buildFoundation() {
        house.setFoundation("ConcreteHouse Foundation");
    }

    @Override
    public void buildStructure() {
        house.setStructure("ConcreteHouse Structure");
    }

    @Override
    public void buildRoof() {
        house.setRoof("ConcreteHouse Roof");
    }

    @Override
    public void buildInterior() {
        house.setInterior("ConcreteHouse Interior");
    }
}

class LuxuryHouseBuilder extends HouseBuilder {
    @Override
    public void buildFoundation() {
        house.setFoundation("LuxuryHouse Foundation");
    }

    @Override
    public void buildStructure() {
        house.setStructure("LuxuryHouse Structure");
    }

    @Override
    public void buildRoof() {
        house.setRoof("LuxuryHouse Roof");
    }

    @Override
    public void buildInterior() {
        house.setInterior("LuxuryHouse Interior");
    }
}

/**
 * 4.创建指导者类 Director
 * 它协调建造过程并返回构建的房屋对象
 */
class Director {
    private HouseBuilder builder;

    public Director(HouseBuilder builder) {
        this.builder = builder;
    }

    public House constructHouse() {
        builder.buildFoundation();
        builder.buildStructure();
        builder.buildRoof();
        builder.buildInterior();
        return builder.getHouse();
    }
}